﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Joel.Vishnu.AccesoDatos.Interfaces;

namespace Joel.Vishnu.AccesoDatos.SQL_Server
{
    public class ServicioDatosFactura : ServicioDatosBase, IServicioDatosFactura
    {
        #region Constructores

        public ServicioDatosFactura()
            : base()
        {
        }

        #endregion

        #region Metodos

        public DataSet Facturas_DevolverTodos()
        {
            try
            {
                SqlCommand cm = new SqlCommand("Facturas_DevolverTodos", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public DataSet Facturas_DevolverPorId(Int32 facturaId)
        {
            try
            {
                SqlCommand cm = new SqlCommand("Facturas_DevolverPorId", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new SqlParameter("@facturaId", facturaId));
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public Int32 Facturas_Insertar(Int32 clienteId, Int32 empleadoId, DateTime fechaHora, Decimal iva)
        {
            try
            {
                SqlCommand cm = new SqlCommand("Facturas_Insertar", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new SqlParameter("@clienteId", clienteId));
                cm.Parameters.Add(new SqlParameter("@empleadoId", empleadoId));
                cm.Parameters.Add(new SqlParameter("@fechaHora", fechaHora));
                cm.Parameters.Add(new SqlParameter("@iva", iva));
                Conexion.Open();
                return Convert.ToInt32(cm.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public Int32 FacturaDetalles_Insertar(Int32 facturaId, Int32 productoId, Decimal precio, Int32 cantidad)
        {
            try
            {
                SqlCommand cm = new SqlCommand("FacturaDetalles_Insertar", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new SqlParameter("@facturaId", facturaId));
                cm.Parameters.Add(new SqlParameter("@productoId", productoId));
                cm.Parameters.Add(new SqlParameter("@precio", precio));
                cm.Parameters.Add(new SqlParameter("@cantidad", cantidad));
                Conexion.Open();
                return Convert.ToInt32(cm.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        #endregion
    }
}
